package com.shenzhenai.composesideeffects.samples

import android.util.Log
import androidx.activity.OnBackPressedCallback
import androidx.activity.OnBackPressedDispatcher
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Switch
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.shenzhenai.composesideeffects.ui.theme.DN_Compose_LearnTheme

/**
 * @author huangzehui
 * @date 19/9/2025 下午10:05 周五
 * @description
 *
 * 82-DisposableEffect
 * https://www.bilibili.com/video/BV1ob4y1a7ad?spm_id_from=333.788.videopod.episodes&vd_source=e07cc8cc94eb22bd76722071b03cd199&p=82
 *
 */

/**
 * 返回的控制方法
 *
 * @param  backDispatcher 返回监听分发器
 */
@Composable
fun BackHandler(backDispatcher: OnBackPressedDispatcher, onBack: () -> Unit) {

    // 当前的返回监听回调
    val currentOnBack by rememberUpdatedState(newValue = onBack)

    //  返回监听回调
    val backCallback by remember {
        mutableStateOf(
            object : OnBackPressedCallback(true) {
                override fun handleOnBackPressed() {
                    // 使用了 rememberUpdatedState 包裹的状态，才可以动态变更，
                    // 否则这里被remember后，重组不会重新创建，那么 onBack 一直是之前的值
                    // 所以需要使用 rememberUpdatedState 包裹，来动态变更
                    currentOnBack()
                }
            }
        )
    }

    DisposableEffect(backDispatcher) {
        // 当 key1 和 当前的组合函数添加到组件树的时候，会触发当前代码块

        // 监听返回监听
        backDispatcher.addCallback(backCallback)
        onDispose {
            // 当前组合函数从组件树移除的时候，会触发当前代码块
            // 移除监听
            backCallback.remove()
        }
    }
}

@Composable
fun DisposableEffectSample(backDispatcher: OnBackPressedDispatcher) {
    var addBackCallback by remember {
        mutableStateOf(false)
    }

    Row(verticalAlignment = Alignment.CenterVertically) {
        Switch(addBackCallback, onCheckedChange = {
            addBackCallback = !addBackCallback
        })

        Text(text = if (addBackCallback) "Add Back Callback" else "Not Add Back Callback")
    }

    if (addBackCallback) {
        BackHandler(backDispatcher) {
            Log.d("young_tesjkjdkf", "onBack")
        }
    }

}


@Preview
@Composable
fun DisposableEffectSamplePreview() {
//    DN_Compose_LearnTheme {
////        Scaffold(modifier = Modifier.fillMaxSize()) {
////            Box(modifier = Modifier.padding(it)) {
////                DisposableEffectSample()
////            }
////        }
//
//    }

}